'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.
/**
 * @fileoverview 战斗奖励
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var buildTypeUtil = require('../util/buildTypeUtil');
var buildType = require('../consts/buildType');

exports.getEconomyBuildGold = function (record_model, damage_build_list) {
    var progress = _getDamageProgress(record_model, damage_build_list);
    return record_model.getDefTotalGold() * progress / 100;
};

exports.getEconomyBuildFood = function (record_model, damage_build_list) {
    var progress = _getDamageProgress(record_model, damage_build_list);
    return record_model.getDefTotalFood() * progress / 100;
};

exports.getDamageProgress = function (record_model, damage_build_list) {
    return _getDamageProgress(record_model, damage_build_list);
};

exports.getStar = function (record_model, damage_build_list) {
    var destroy_per = 0;
    var def_build_list = record_model.getDefBuildList();
    var is_destroy_nexus = false;
    _.each(damage_build_list, function (damage_build) {
        // 判断是否是主城,如果主城未被打完,只算0星
        if (buildTypeUtil.getBuildType(damage_build.build_id) == buildType.WALL &&
            damage_build.hp === 0) {
            is_destroy_nexus = true;
        }

        // 计算占比
        _.some(def_build_list, function (def_build) {
            if (def_build.build_id == damage_build.build_id) {
                destroy_per += def_build.per / 100;
                return true;
            }
        });
    });

    // 如果主城没摧毁算0星
    if (!is_destroy_nexus) {
        return 0;
    }

    if (destroy_per <= 0.79) {
        return 1;
    } else if (destroy_per < 0.95) {
        return 2;
    } else {
        return 3;
    }
};

/**
 * 奖杯数量
 */
exports.getCup = function (star, atk_lv, def_lv) {
    var first_value;
    if (Math.abs(atk_lv - def_lv) < 4) {
        first_value = 10;
    } else if (atk_lv - def_lv < 0) {
        first_value = 7 - atk_lv + def_lv;
    } else {
        first_value = 13 - atk_lv + def_lv;
    }
    first_value = Math.max(first_value, 5);
    first_value = Math.min(first_value, 15);
    switch (star) {
    case 1:
        {
            return first_value - 3;
        }
    case 2:
        {
            return first_value - 1;
        }
    case 3:
        {
            return first_value;
        }
    default:
        {
            console.error('star error:' + star);
            return 0;
        }
    }
};

/**
 * 获取经验
 * @param  {[type]} record_model      [description]
 * @param  {[type]} damage_build_list [description]
 * @return {[type]}                   [description]
 */
exports.getDestroyBuildExp = function (record_model, damage_build_list) {
    var total_exp = record_model.getDefTotalExp();
    var def_build_list = record_model.getDefBuildList();
    var earn_exp = 0;
    _.each(damage_build_list, function (damage_build) {
        // 如果血量不是为0,不算经验
        if (damage_build.hp !== 0) {
            return;
        }

        _.some(def_build_list, function (def_build) {
            if (def_build.build_id == damage_build.build_id) {
                earn_exp += total_exp * def_build.per / 100;
                return true;
            }
        });
    });
    return earn_exp;
};

var _getDamageProgress = function (record_model, damage_build_list) {
    var total_gold = record_model.getDefTotalGold();
    var def_build_list = record_model.getDefBuildList();
    var progress = 0;
    var destroy_build_num = 0;
    _.each(damage_build_list, function (damage_build) {
        _.some(def_build_list, function (def_build) {
            if (def_build.build_id != damage_build.build_id) {
                return;
            }

            if (damage_build.hp < 0 ||
                damage_build.hp > def_build.attr.hp) {
                return true;
            } else if (damage_build.hp === 0) {
                destroy_build_num++;
            }

            progress +=
                (def_build.attr.hp - damage_build.hp) /
                def_build.attr.hp *
                def_build.per;

            return true;
        });
    });

    if (destroy_build_num == def_build_list.length) {
        progress = 100;
    }

    return progress;
};
